// 导航栏滚动效果
window.addEventListener('scroll', function() {
  const navbar = document.getElementById('navbar');
  if (window.scrollY > 20) {
    navbar.classList.add('scrolled');
  } else {
    navbar.classList.remove('scrolled');
  }
});

// 用户菜单切换
const userMenuBtn = document.getElementById('userMenuBtn');
const userMenu = document.getElementById('userMenu');

userMenuBtn.addEventListener('click', function() {
  userMenu.classList.toggle('hidden');
});

// 移动端菜单切换
const mobileMenuBtn = document.getElementById('mobileMenuBtn');
const mobileMenu = document.getElementById('mobileMenu');

mobileMenuBtn.addEventListener('click', function() {
  mobileMenu.classList.toggle('hidden');
});

// 点击页面其他地方关闭下拉菜单
document.addEventListener('click', function(event) {
  if (!userMenuBtn.contains(event.target) && !userMenu.contains(event.target)) {
    userMenu.classList.add('hidden');
  }
  
  if (!mobileMenuBtn.contains(event.target) && !mobileMenu.contains(event.target)) {
    mobileMenu.classList.add('hidden');
  }
});

// 个人信息表单提交
const profileForm = document.getElementById('profileForm');
const cancelProfileBtn = document.getElementById('cancelProfileBtn');
const saveSuccessToast = document.getElementById('saveSuccessToast');

// 保存表单初始值用于取消操作
const initialProfileValues = {
  username: document.getElementById('username').value,
  college: document.getElementById('college').value,
  major: document.getElementById('major').value,
  grade: document.getElementById('grade').value,
  class: document.getElementById('class').value,
  email: document.getElementById('email').value,
  phone: document.getElementById('phone').value,
  bio: document.getElementById('bio').value
};

profileForm.addEventListener('submit', function(e) {
  e.preventDefault();
  
  // 模拟表单提交
  setTimeout(() => {
    showSaveSuccessToast();
  }, 500);
});

cancelProfileBtn.addEventListener('click', function() {
  // 恢复初始值
  document.getElementById('username').value = initialProfileValues.username;
  document.getElementById('college').value = initialProfileValues.college;
  document.getElementById('major').value = initialProfileValues.major;
  document.getElementById('grade').value = initialProfileValues.grade;
  document.getElementById('class').value = initialProfileValues.class;
  document.getElementById('email').value = initialProfileValues.email;
  document.getElementById('phone').value = initialProfileValues.phone;
  document.getElementById('bio').value = initialProfileValues.bio;
});

// 密码表单提交
const passwordForm = document.getElementById('passwordForm');
const cancelPasswordBtn = document.getElementById('cancelPasswordBtn');

passwordForm.addEventListener('submit', function(e) {
  e.preventDefault();
  
  const currentPassword = document.getElementById('currentPassword').value;
  const newPassword = document.getElementById('newPassword').value;
  const confirmPassword = document.getElementById('confirmPassword').value;
  
  // 简单验证
  if (!currentPassword || !newPassword || !confirmPassword) {
    alert('请填写所有密码字段');
    return;
  }
  
  if (newPassword !== confirmPassword) {
    alert('新密码和确认密码不一致');
    return;
  }
  
  if (newPassword.length < 8) {
    alert('新密码长度至少8位');
    return;
  }
  
  // 模拟表单提交
  setTimeout(() => {
    showSaveSuccessToast();
    passwordForm.reset();
  }, 500);
});

cancelPasswordBtn.addEventListener('click', function() {
  passwordForm.reset();
});

// 显示保存成功提示
function showSaveSuccessToast() {
  saveSuccessToast.classList.add('show');
  saveSuccessToast.classList.remove('opacity-0', 'translate-y-4');
  
  setTimeout(() => {
    saveSuccessToast.classList.remove('show');
    saveSuccessToast.classList.add('opacity-0', 'translate-y-4');
  }, 3000);
}

// 模拟数据加载动画
window.addEventListener('load', function() {
  setTimeout(() => {
    document.querySelectorAll('.animate-fade-in').forEach(el => {
      el.style.opacity = '1';
      el.style.transform = 'translateY(0)';
    });
  }, 300);
});
    